<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxMp\WxMpListService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 微信应用控制器
 * @package App\Http\Controllers\Admin
 */
class WxMpListController extends BaseController
{
    private $mpListSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->mpListSer = new WxMpListService();
    }

    /**
     * 应用列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            //状态条件
            $query = \App\Models\WxMpList::select('*');
            $type = $request->input('type', -1);
            if ($type > -1){
                $query->where('type', $type);
            }
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('nick_name', 'like', '%'.$keyword.'%');
            }
            $rows = $query->paginate($pageSize);
            if ($rows) {
                foreach ($rows as $k => $v) {
                    //服务号回调地址
                    if ($v['type'] == WxMpListService::TYPE_WECHAT){
                        $v['callback_url'] = route('callback.wechat.serve', ['appid'=>$v['appid']]);
                    }elseif ($v['type'] == WxMpListService::TYPE_APPLET){
                        $v['callback_url'] = route('callback.applet.serve', ['appid'=>$v['appid']]);
                    }elseif ($v['type'] == WxMpListService::TYPE_OPEN_PLATFORM){
                        $v['callback_url'] = route('callback.open.serve', ['appid'=>$v['appid']]);
                    }else{
                        $v['callback_url'] = '';
                    }
                    $rows[$k] = $v;
                }
            }
            return $rows;
        }
        $this->viewData['typeList'] = $this->mpListSer->getTypeList();
        return view('admin.wxmplist.index', $this->viewData);
    }

    /**
     * 保存应用
     * @param \App\Http\Requests\AdminWxMpListPost $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxMpListPost $request){
        try {
            $this->mpListSer->save($request);
            return $this->showMsg('success', '保存应用成功', 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除应用
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $this->mpListSer->del($request->id ?? 0);
            return $this->showMsg('success', '删除应用成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置当前用户选择的微信应用
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function setUserWxMp($appid)
    {
        try{
            if (empty($appid)){
                throw new \Exception('请选择微信应用');
            }
            \Auth::user()->setWxMpCurrent($appid);
            return $this->showMsg('success', '切换成功', [],'admin.index.index');
        }catch (\Exception $e){
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
